#include"StdThread.h"
#include"SystemHead.h"
#include<pthread.h>

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
int money=10000;
void *thread1(void *arg)
{
    printf("zhangsan: current money:%d\n",money);

    pthread_mutex_lock(&mutex);

    sleep(1);
    int MyMoney=money;
    printf("zhangsan take the money!\n");
    sleep(1);

    if(MyMoney>=10000)
    {
        money=0;
        printf("zhangsan take 10000!current money:%d\n",money);
    }
    else
    {
        printf("zhangsan take 0!current money:%d\n",money);
    }
    pthread_mutex_unlock(&mutex);
    return NULL;
}

void *thread2(void *arg)
{
    printf("lisi: current money:%d\n",money);
    sleep(1);

    pthread_mutex_lock(&mutex);

    int MyMoney=money;
    printf("lisi take the money!\n");
    sleep(1);

    if(MyMoney>=10000)
    {
        money=0;
        printf("lisi take 10000!current money:%d\n",money);
    }
    else
    {
        printf("lisi take 0!current money:%d\n",money);
    }
    pthread_mutex_unlock(&mutex);
    return NULL;
}

int main()
{
    pthread_mutex_init(&mutex,NULL);

    thread *t1 = InitThread(thread1,NULL);
    thread *t2 = InitThread(thread2,NULL);

    JoinThread(t1);
    JoinThread(t2);

    pthread_mutex_destroy(&mutex);
    return 0;
}